﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Net;
using System.Xml;
using iPhOwa.Exchange.ContentClasses;

namespace iPhOwa.Exchange.WebDav.Methods
{
	/// <summary>
	/// Gets the attachments for a specific path.
	/// </summary>
	internal sealed class XMSEnumAtts : Method
	{
		public XMSEnumAtts(Uri path, OwaSession session) : base(path, session) {}

		public override string Name
		{
			get { return "X-MS-ENUMATTS"; }
		}

		protected override string BuildCommand()
		{
			return string.Empty;
		}

		protected override void AddHeaders(HttpWebRequest request)
		{
			return;
		}

		public ICollection<Attachment> Run()
		{
			XmlDocument document = ReadXml(Execute().GetResponseStream());

			XmlNodeList responses = document.GetElementsByTagName("a:response");

			ICollection<Attachment> items = new Collection<Attachment>();

			foreach(XmlNode response in responses)
			{
				PropertyDictionary propertyHash = ReadProperties(response.ChildNodes);

				var item = ItemFactory.Create(ContentClassNames.Attachment) as Attachment;
				item.Load(propertyHash);
				items.Add(item);
			}

			return items;
		}
	}
}